using System;
using Microsoft.AspNetCore.Mvc;
using ApiProject.Api.Entity;
using System.Linq;
using ApiProject.Api.Repository;
using Microsoft.AspNetCore.Hosting;
using ApiProject.Api.Uitls;
using ApiProject.Api.Utils;
using ApiProject.Api.PramaModel;

// 总订单管理
namespace ApiProject.Api.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class OrdersAdminController : ControllerBase
    {
        private IRepository<Orders> _orders;
        private IRepository<OrderInfo> _orderInfo;
        private IRepository<ReceivingIdInfo> _receivingIdInfo;
        private IRepository<Commodities> _commodities;
        private IRepository<CommodityDetails> _commodityDetails;

        private IRepository<CommodityDetailPicture> _commodityDetailPicture;


        public OrdersAdminController(IRepository<Orders> orders, 
        IRepository<OrderInfo> orderInfo, IRepository<ReceivingIdInfo> receivingIdInfo, IRepository<Commodities> commodities,
         IRepository<CommodityDetails> commodityDetails,
         IRepository<CommodityDetailPicture> commodityDetailPicture)
        {
            _orders = orders;
            _orderInfo = orderInfo;
            _receivingIdInfo = receivingIdInfo;
            _commodities = commodities;
            _commodityDetails = commodityDetails;
            _commodityDetailPicture=commodityDetailPicture;
        }


        //获取所有的订单信息
        //Get 
        [HttpGet]
        public dynamic Get([FromQuery] Pages page)
        {
            var pageIndex = page.pageIndex;
            var pageSize = page.pageSize;
            //var allpic = _orders.Table.Where(x => x.IsDelete == false && x.IsEnable == true).ToList();

            var ordernum = _orderInfo.Table.Select(p => p.OrderNumber).Distinct().ToList();

            var orderss = from order in _orders.Table
                          join orderInfo in _orderInfo.Table
                           on order.Id equals orderInfo.OrderId
                          where order.Id == orderInfo.OrderId
                          orderby order.Id
                          select new
                          {
                              OrderNumber = orderInfo.OrderNumber,
                              OrderPrice = orderInfo.OrderPrice,
                              IsSend = orderInfo.IsSend,
                              IsPay = orderInfo.IsPay,
                              PayTime = orderInfo.PayTime
                          };

            var i = from a in orderss.GroupBy(x => new {x.OrderNumber,x.IsPay,x.IsSend,x.PayTime})
                    select new
                    {
                        allPrice = a.Sum(x => x.OrderPrice),
                        OrderNum = a.Key.OrderNumber,
                        IsPay = a.Key.IsPay,
                        IsSend = a.Key.IsSend,
                        PayTime = a.Key.PayTime,
                    };



            Console.WriteLine(i);

            var u = i.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return JsonHelper.Serialize(new
            {
                Code = 200,
                Data = new { Data = u, pager = new { pageIndex, pageSize, rowsTotal = orderss.Count() } },
                Msg = "获取成功"
            });
        }


        //获取指定订单详情信息
        [HttpGet, Route("details/{id}")]
        public dynamic GetOrder(string id)
        {
            // var orders = _orderInfo.GetById(id);

            // if (orders == null)
            // {
            //     return JsonHelper.Serialize(new
            //     {
            //         Code = 1000,
            //         Data = "",
            //         Msg = "获取失败"
            //     });
            // }
            // else
            // {
            var details = from order in _orders.Table
                          join orderInfo in _orderInfo.Table
                           on order.Id equals orderInfo.OrderId
                          join receivingInfo in _receivingIdInfo.Table
                          on order.UserId equals receivingInfo.Id
                          join Commoditie in _commodities.Table
                          on order.CommodityId equals Commoditie.Id
                          join commodityDetail in _commodityDetails.Table
                        on order.CommodityId equals commodityDetail.CommodityId
                        // join commPicture in _commodityDetailPicture.Table
                        // on order.CommodityId equals commPicture.CommodityId
                          where orderInfo.OrderNumber == id 
                        //   where commPicture.IsCover== true
                          select new
                          {
                              OrderNumber = orderInfo.OrderNumber,
                              Recipients = receivingInfo.Recipients,
                              PayTime = orderInfo.PayTime,
                              Telephone = receivingInfo.Telephone,
                              Address = receivingInfo.Address,
                              CommodityName = Commoditie.CommodityName,
                              Comnum=orderInfo.Comnum,
                              price = commodityDetail.Price * orderInfo.Comnum,
                               OrderPrice = orderInfo.OrderPrice,
                            //    CommodityPicturePath=commPicture.CommodityPicturePath,
                               SpeChildId=commodityDetail.SpeChildId
                          };

               

            return JsonHelper.Serialize(new
            {
                Code = 200,
                Data = details,
                Msg = "获取成功"
            });




            //     }
        }





    }
}